package com.sgb.kjwl.viewmodel.material;

import android.util.Log;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.supplier3.SupplierRecommendEntity;
import com.sgb.kjwl.model.entity.user.PromoteGoodsEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.fragment.material.GoodsDiatanceListFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.functions.Consumer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


@Deprecated
public class DistanceListModel extends BaseViewModel {
    private GoodsDiatanceListFragment fragment;
    private HomeRepertory repertory;
    private final int pageLimit = 10;
    private int page = 1;
    private int size = 10;
    public DistanceListModel(GoodsDiatanceListFragment fragment, HomeRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }
////    获取状态类型
//    public void getEnquiryType() {
//        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
//        APIManager.getInstance().getCommonAPI().getEnquiryType().enqueue(new Callback<BaseEntity<List<EnquiryTypeEntity>>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Response<BaseEntity<List<EnquiryTypeEntity>>> response) {
//                DialogHelper.dismissProgressDialog();
//                if (response.body() == null)
//                    return;
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    if (response.body().getData() == null)
//                        return;
//                    fragment.getEnquirtTypeList(response.body().getData());
//                } else {
//                    MToast.showToast(fragment.getActivity(), response.message());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
//                MToast.showToast(fragment.getActivity(), t.getMessage());
//            }
//        });
//    }
//    //    获取用户类型
//    public void getUserType() {
//        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
//        APIManager.getInstance().getCommonAPI().getUserType().enqueue(new Callback<BaseEntity<List<EnquiryTypeEntity>>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Response<BaseEntity<List<EnquiryTypeEntity>>> response) {
//                DialogHelper.dismissProgressDialog();
//                if (response.body() == null)
//                    return;
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    if (response.body().getData() == null)
//                        return;
//                    fragment.getUserTypeList(response.body().getData());
//
//                } else {
//                    MToast.showToast(fragment.getActivity(), response.message());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
//                MToast.showToast(fragment.getActivity(), t.getMessage());
//            }
//        });
//    }
//
////    获取排序类型
//    public void getSortType() {
//        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
//        APIManager.getInstance().getCommonAPI().getSortType().enqueue(new Callback<BaseEntity<List<EnquiryTypeEntity>>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Response<BaseEntity<List<EnquiryTypeEntity>>> response) {
//                DialogHelper.dismissProgressDialog();
//                if (response.body() == null)
//                    return;
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    if (response.body().getData() == null)
//                        return;
//                    fragment.getSortTypeList(response.body().getData());
//                } else {
//                    MToast.showToast(fragment.getActivity(), response.message());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
//                MToast.showToast(fragment.getActivity(), t.getMessage());
//            }
//        });
//    }
////获取数据列表信息
//    public void getDemandHallList(String userid, String sortid, String enquiryid, String fcatName,String searchname,int page ) {
//        Map map =new HashMap();
//        if (!TextUtils.isEmpty(userid)){
//            map.put("authMethod",userid);
//        }
//        map.put("limit",pageLimit);
//        map.put("page",page);
//        map.put("sortType",sortid);
//        if (!TextUtils.isEmpty(enquiryid)){
//            map.put("status",enquiryid);
//        }
//        map.put("fcatName",fcatName+"");
//        map.put("tcatName",searchname+"");
//        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
//        APIManager.getInstance().getCommonAPI().getDemandHallList(map).enqueue(new Callback<BaseEntity<ListResponse<List<DemandHallListEntity>>>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<ListResponse<List<DemandHallListEntity>>>> call, Response<BaseEntity<ListResponse<List<DemandHallListEntity>>>> response) {
//                DialogHelper.dismissProgressDialog();
//                if (response.body() == null)
//                    return;
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    if (response.body().getData() == null)
//                        return;
//                    fragment.setDemandHallList(response.body().getData().getList());
//                } else {
//                    MToast.showToast(fragment.getActivity(), response.message());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<ListResponse<List<DemandHallListEntity>>>> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
//                MToast.showToast(fragment.getActivity(), t.getMessage());
//            }
//        });
//    }
////需求列表详情界面
//public void showUrl(String no) {
//    APIManager.getInstance().getLookingforAJobAPI().getUrlSetting("enquiryDetail").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
//        @Override
//        public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
//            if (response.body() == null || response.body().getState() == null) {
//                return;
//            }
//            if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                if (!TextUtils.isEmpty(response.body().getData().getUrl())){
//                    String url = response.body().getData().getUrl() + "?no=" + no;
//                    WebViewX5HostActivity.start(fragment.getActivity(), url);
//
//                    //存储商品跳转h5标记
//                    SharedPreferenceUtil.getInstance().saveString("EVENT_SHOPPING_TYPE", "event_shopping_list");
//                }else {
//                    WebViewX5HostActivity.start(fragment.getActivity(), "");
//                }
//            } else {
//                MToast.showToast(fragment.getActivity(), response.body().getMsg());
//            }
//        }
//
//        @Override
//        public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
//        }
//    });
//}



    //获取供货商列表数据
    public void getSupplierListModel(Map map, boolean isLoadMore) {
        Log.e("tagmap",map.toString() );
        page = isLoadMore ? page + 1 : 1;
        map.put("areaCode", repertory.getAreaCode());
        map.put("page", page);
        map.put("limit", size);
        repertory.getSupplierList(map)
                .subscribe(new Consumer<BaseEntity<ListResponse<List<SupplierRecommendEntity>>>>() {
                    @Override
                    public void accept(BaseEntity<ListResponse<List<SupplierRecommendEntity>>> listResponseBaseEntity) throws Exception {
                        if (listResponseBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            fragment.showList(listResponseBaseEntity.getData().getList(), isLoadMore);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    //获取供货商列表数据
//    public void getGoodsModel(Map map, boolean isLoadMore) {
//        Log.e("tagmap",map.toString() );
//        page = isLoadMore ? page + 1 : 1;
//        map.put("areaCode", repertory.getAreaCode());
//        map.put("page", page);
//        map.put("limit", size);
//        repertory.getGoodsList(map)
//                .subscribe(new Consumer<BaseEntity<ListResponse<List<SupplierRecommendEntity>>>>() {
//                    @Override
//                    public void accept(BaseEntity<ListResponse<List<SupplierRecommendEntity>>> listResponseBaseEntity) throws Exception {
//                        if (listResponseBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
//                            fragment.showList(listResponseBaseEntity.getData().getList(), isLoadMore);
//                        }
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
//                    }
//                });
//    }


    public void getDistanceModel() {
        repertory.getDistance()
                .subscribe(new Consumer<BaseEntity<List<DictEntity3>>>() {
                    @Override
                    public void accept(BaseEntity<List<DictEntity3>> listBaseEntity) throws Exception {
                        if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            fragment.showDistancePopWindow(listBaseEntity.getData());
//                            fragment.showTypePopWindow(listBaseEntity.getData());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }


//    public void getSupplierCityModel() {
//        repertory.getSupplierCityData(repertory.getAreaCode())
//                .subscribe(new Consumer<BaseEntity<List<DictEntity3>>>() {
//                    @Override
//                    public void accept(BaseEntity<List<DictEntity3>> listBaseEntity) throws Exception {
//                        if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
//                            fragment.showCityPopWindow(listBaseEntity.getData());
//                        }
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
//                    }
//                });
//    }

//    public void getSupplierDistanceModel() {
//        repertory.getSupplierDistanceData()
//                .subscribe(new Consumer<BaseEntity<List<DictEntity3>>>() {
//                    @Override
//                    public void accept(BaseEntity<List<DictEntity3>> listBaseEntity) throws Exception {
//                        if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
//                            activity3.showDistancePopWindow(listBaseEntity.getData());
//                            activity3.showTypePopWindow(listBaseEntity.getData());
//                        }
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
//                    }
//                });
//    }

//    public void getSupplierSortModel() {
//        repertory.getSupplierSortData()
//                .subscribe(new Consumer<BaseEntity<List<DictEntity3>>>() {
//                    @Override
//                    public void accept(BaseEntity<List<DictEntity3>> listBaseEntity) throws Exception {
//                        if (listBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
//                            fragment.showSortPopWindow(listBaseEntity.getData());
//                        }
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
//                    }
//                });
//    }

    //获取供货商地图数据
    public void getSupplierMapModel(Map map) {
        Log.e("tag地图数据map",map.toString() );
        map.put("current", page);
        map.put("limit", 10);
        repertory.getSupplierMapData(map)
                .subscribe(new Consumer<BaseEntity<ListResponse<List<SupplierRecommendEntity>>>>() {
                    @Override
                    public void accept(BaseEntity<ListResponse<List<SupplierRecommendEntity>>> listResponseBaseEntity) throws Exception {
                        if (listResponseBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            fragment.showMap(listResponseBaseEntity.getData().getList());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });

    }


    //获取供货商地图数据
    public void getGoodsMapModel(Map map) {
        Log.e("tag地图数据map",map.toString() );
        map.put("current", page);
        map.put("limit", 10);
//        repertory.getGoodsMapData(map)
//                .subscribe(new Consumer<BaseEntity<ListResponse<List<SupplierRecommendEntity>>>>() {
//                    @Override
//                    public void accept(BaseEntity<ListResponse<List<SupplierRecommendEntity>>> listResponseBaseEntity) throws Exception {
//                        if (listResponseBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
//                            fragment.showMap(listResponseBaseEntity.getData().getList());
//                        }
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
//                    }
//                });

    }

    //获取商品列表
    public void getGoodsModel(HashMap<String,Object> map, boolean isLoadMore) {
        Log.e("tagmap",map.toString() );
        page = isLoadMore ? page + 1 : 1;
        map.put("areaCode", repertory.getAreaCode());
        map.put("page", page);
        map.put("limit", size);
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().findPromoteGoods(map).enqueue(new Callback<BaseEntity<List<PromoteGoodsEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PromoteGoodsEntity>>> call, Response<BaseEntity<List<PromoteGoodsEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null)
                        return;
                    fragment.showGoodsList(response.body().getData(), isLoadMore);
                } else {
                    MToast.showToast(fragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PromoteGoodsEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getActivity(), t.getMessage());
            }
        });
    }
}
